﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.admin
{
    public partial class populateClass : System.Web.UI.Page
    {
        int class_id;

        protected void Page_Load(object sender, EventArgs e)
        {

            class_id = Convert.ToInt32(Session["class_id"]);
            if (!Page.IsPostBack)
            {
                populatePage();
            }
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            //populate attending students list box
            var attendingStudents = (from s in db.attendings
                                     join ui in db.user_info on s.user_id equals ui.user_id
                                     where s.class_id == class_id
                                     orderby ui.user_info_first_name ascending
                                     select new
                                     {
                                         user_id = s.user_id,
                                         name = ui.user_info_first_name + " " + ui.user_info_last_name
                                     });
            ListBox1.DataSource = attendingStudents;
            ListBox1.DataTextField = "name";
            ListBox1.DataValueField = "user_id";
            ListBox1.DataBind();

            //populating all students list box
            var studentResult = (from s in db.lpusers
                                 join ui in db.user_info on s.user_id equals ui.user_id
                                 where s.user_level == 0
                                 orderby ui.user_info_first_name ascending
                                 select new
                                 {
                                     user_id = s.user_id,
                                     name = ui.user_info_first_name + " " + ui.user_info_last_name
                                 });
            
            ListBox2.DataSource = studentResult;
            ListBox2.DataTextField = "name";
            ListBox2.DataValueField = "user_id";
            ListBox2.DataBind();

            foreach (ListItem l in ListBox1.Items)
            {
                ListBox2.Items.Remove(l);
            }

            //Populate Lables
            var classInfo = (from c in db.classes
                             join ui in db.user_info on c.user_id equals ui.user_id
                             join s in db.subjects on c.subject_id equals s.subject_id
                             where c.class_id == class_id
                             select new {
                                class_name = c.class_name,
                                class_description = c.class_description,
                                name = ui.user_info_first_name + " " + ui.user_info_last_name,
                                subject = s.subject_name
                             
                             }).SingleOrDefault();

            nameLabel.Text = classInfo.class_name;
            descriptionLabel.Text = classInfo.class_description;
            teacherLabel.Text = classInfo.name;
            subjectLabel.Text = classInfo.subject;
        }

        private void addAttending()
        {
            if(ListBox2.SelectedItem != null) {
                int id = Convert.ToInt32(ListBox2.SelectedValue);
                ListBox1.Items.Add(ListBox2.SelectedItem);
                ListBox2.Items.Remove(ListBox2.SelectedItem);

                tsu2011db4Entities1 db = new tsu2011db4Entities1();
                attending a = new attending();
                a.class_id = class_id;
                a.user_id = id;
                db.attendings.AddObject(a);
                db.SaveChanges();
                Response.Redirect("populateClass.aspx");
            }
        }

        private void removeAttending()
        {
            if (ListBox1.SelectedItem != null)
            {
                int id = Convert.ToInt32(ListBox1.SelectedValue);
                ListBox2.Items.Add(ListBox1.SelectedItem);
                ListBox1.Items.Remove(ListBox1.SelectedItem);

                tsu2011db4Entities1 db = new tsu2011db4Entities1();
                var v = (from a in db.attendings
                         where a.user_id == id
                         where a.class_id == class_id
                         select a).SingleOrDefault();

                db.attendings.DeleteObject(v);
                db.SaveChanges();
                Response.Redirect("populateClass.aspx");
            }
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            addAttending();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            removeAttending();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("class.aspx");
        }
        


    }
}